﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Purchase.aspx.cs" Culture="en-GB" Inherits="Main_Accounts_VoucherEntry_Purchase" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxControl" %>
<%@ Register Assembly="CalendarExtenderPlus" Namespace="AjaxControlToolkitPlus" TagPrefix="advancedCalender" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Contra</title>
     <link href="../../../Styles/accounts.css" rel="stylesheet" type="text/css" />
     <script type="text/javascript">     
        function isNumberKey(control_id, evt)
        {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 45 || charCode > 47))
            return false;

            if (charCode == 46)
            {
            var dotcount = document.getElementById(control_id).value.split('.').length - 1;
            if (dotcount >= 1)
            {
            return false;
            }
            }
            return true;
        }
        
        function RemoveZeroOnFocusCredit(ctl_id)
        {            
            var totalCreditAmt=document.getElementById("txtTotalCredit").value;  
            var totalDebitAmt=document.getElementById("txtTotalDebit").value;
            var txtCredit=document.getElementById(ctl_id);            
            if(txtCredit.value=='0.00' && txtCredit.disabled!=true)
            {
                if(parseFloat(totalDebitAmt)>parseFloat(totalCreditAmt))
                {
                    txtCredit.value = parseFloat(parseFloat(totalDebitAmt) - parseFloat(totalCreditAmt)).toFixed(2); 
                }
                else if(parseFloat(totalDebitAmt)==parseFloat(totalCreditAmt))                
                {
                    txtCredit.value=''; 
                }
                else
                {
                    txtCredit.value='';
                }                                               
            }
        } 
        function RemoveZeroOnFocusDebit(ctl_id)
        {          
            var totalCreditAmt=document.getElementById("txtTotalCredit").value;  
            var totalDebitAmt=document.getElementById("txtTotalDebit").value; 
            var txtDebit=document.getElementById(ctl_id);            
            if(txtDebit.value=='0.00' && txtDebit.disabled!=true)
            {
                if(parseFloat(totalCreditAmt)>parseFloat(totalDebitAmt))
                {
                    txtDebit.value = parseFloat(parseFloat(totalCreditAmt) - parseFloat(totalDebitAmt)).toFixed(2);
                }
                else if(parseFloat(totalCreditAmt)==parseFloat(totalDebitAmt))
                {
                    txtDebit.value='';
                }
                else
                {
                    txtDebit.value='';
                }                                              
            }           
        } 
        
        function SetZeroOnBlurDebit()
        {
            var totalDebitAmt=0; 
            var Count=document.getElementById("hidControl").value;           
            for(var i=1;i<=Count;i++)
            {                  
                var zero=0;
                var txtDebit=document.getElementById("txtDebit"+i);               
                if(txtDebit.value=='')
                {
                    if(txtDebit.disabled==false)
                    {
                        txtDebit.value=zero.toFixed(2);
                        totalDebitAmt+=zero;
                    }
                    else
                    {
                        totalDebitAmt+=zero;
                    }                                                     
                }
                else 
                {  
                    var debitAmt=parseFloat(txtDebit.value);                    
                    txtDebit.value=debitAmt.toFixed(2); 
                    totalDebitAmt+=debitAmt;                   
                }                                           
            } 
            document.getElementById("txtTotalDebit").value=totalDebitAmt.toFixed(2);             
                
        }
        
        function SetZeroOnBlurCredit()
        {
            var totalCreditAmt=0;
            var Count=document.getElementById("hidControl").value;           
            for(var i=1;i<=Count;i++)
            {  
               
                var zero=0;               
                var txtCredit=document.getElementById("txtCredit"+i);                
                if(txtCredit.value=='')
                {
                    if(txtCredit.disabled==false)
                    {
                        txtCredit.value=zero.toFixed(2);
                        totalCreditAmt+=zero;
                    }
                    else
                    {
                        totalCreditAmt+=zero;
                    }                                                                 
                }
                else 
                {  
                    var creditAmt =parseFloat(txtCredit.value);
                    txtCredit.value=creditAmt.toFixed(2); 
                    totalCreditAmt+=creditAmt;                   
                }                            
            }
            document.getElementById("txtTotalCredit").value=totalCreditAmt.toFixed(2);  
                
        }
        function validateAmt()
        {
            var flag=true;
            var totalDebitAmt=document.getElementById("txtTotalDebit").value;
            var totalCrditAmt=document.getElementById("txtTotalCredit").value;
            var txtVouNo=document.getElementById("txtVouNo").value;
            var txtDate=document.getElementById("txtDate").value;
            
            if(txtVouNo=='' || txtVouNo=='0')
            {
                 alert('Specified Voucher No!');
                 flag=false;  
            }
            else if(txtDate=='')
            {
                alert('Specified Voucher Date!');
                flag=false; 
            }            
            
            var Count=document.getElementById("hidControl").value;           
            for(var i=1;i<=Count;i++)
            {               
                var ddlLedger=document.getElementById("ddlLedger"+i);                
                if(ddlLedger.selectedIndex==0)
                {
                     alert('Specified Ledger at row No. '+i);
                     flag=false;                                                           
                }
            }
            if(parseFloat(totalDebitAmt)>0 || parseFloat(totalCrditAmt)>0)
            {
                if(parseFloat(totalDebitAmt)!=parseFloat(totalCrditAmt))
                {
                    alert('Amount not maching !');
                    flag=false;
                }               
            }
            else
            {
                 alert('Amount should not be zero');
                 flag=false;
            }
                
            return flag;            
        }
        
        function EnableCash()
        {  
            var radio = document.getElementsByName("radioCashCredit");
            var divCash=document.getElementById("divCash");
            for (var i = 0; i < radio.length; i++)
            {
                if (radio[i].checked)
                {   
                    if(radio[i].value=='1')
                    {
                        divCash.style.visibility="hidden";
                    }
                    else
                    {
                      divCash.style.visibility="visible";  
                    }
                   
                }
            }
        }
        
        function confirmDelete()
        {
            var Count=document.getElementById("hidControl").value; 
            if(Count>1)
            {
                var flag=confirm('Are you sure to Delete Last Row');
                if(flag==true)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        function ValidateRepeatLedger(id) {
            var flag = true;
            var count = document.getElementById("hidControl").value;
            var currentLedger = document.getElementById(id);
            var lastNo = id.toString().substring(9);
            var currentValue = currentLedger.options[currentLedger.selectedIndex].value;
            for (var i = 1; i <= count; i++) {
                var previousLedger = document.getElementById("ddlLedger" + i);
                var previousValue = previousLedger.options[previousLedger.selectedIndex].value;
                if (currentLedger != previousLedger) {
                    if (currentValue == previousValue && previousValue != '-1' && currentValue != '-1') {
                        alert('Specifed Ledger can not be same');
                        currentLedger.selectedIndex = '0';
                        document.getElementById("txtBalance" + lastNo).value = '';
                        flag = false;
                        break;
                    }
                }
            }

            if (flag == true) {
                setTimeout('__doPostBack(\'' + id + '\',\'\')', 0);
            }
            return flag;
        }
        function EnabledTabKey(id, e) {
            var keycode;
            if (window.event) {
                keycode = window.event.keyCode;
            }
            else if (e) {
                keycode = e.which;
            }
            else {
                return true;
            }

            if (keycode == 9) {
                // if is the tab key                
                return true;
            }
            else {
                return false;
            }
        }
        </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
     <div style="margin-left:5%; margin-right:5%; width:90%; text-align:center;">
        <asp:Label ID="lblMsg" runat="server" Font-Bold="true"></asp:Label>
    </div>
    <div style="margin-left:5%; margin-right:5%; width:90%;">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <table style="width:100%; border:solid 1px #818181;">
            <tr>
                <td colspan="4" style="text-align:center;  border-bottom:solid 1px #818181;" class="headtd" >
                    Voucher Entry For Purchase
                </td>
            </tr>
             <tr>
                <td style="text-align:left; width:15%">
                    Voucher No.
                </td>
                <td style="text-align:left; width:35%">
                    <asp:TextBox ID="txtPrefix" runat="server" CssClass="input" Width="80px" Visible="false" Enabled="false" ToolTip="Prefix"></asp:TextBox>
                    <asp:TextBox ID="txtVouNo" runat="server" CssClass="inputnumber" Width="80px" onkeypress="return isNumberKey(this.id,event)" ToolTip="Voucher Number"></asp:TextBox>
                    <asp:TextBox ID="txtSuffix" runat="server" CssClass="input" Width="80px" Visible="false" Enabled="false" ToolTip="Suffix"></asp:TextBox>
                </td>
                <td style="text-align:left; width:10%">
                    Date:
                </td>
                <td style="text-align:left; width:40%">
                   <%-- <asp:TextBox ID="txtDate" runat="server" CssClass="input" Width="80px" ToolTip="Voucher Date"></asp:TextBox>                    
                    <ajaxControl:CalendarExtender ID="extCal" runat="server" TargetControlID="txtDate" Format="dd/MM/yyyy" ></ajaxControl:CalendarExtender>
                    <ajaxControl:MaskedEditExtender ID="extMask" runat="server" TargetControlID="txtDate" MaskType="Date" Mask="99/99/9999" CultureName="en-GB" AutoComplete="true"></ajaxControl:MaskedEditExtender>
                    <ajaxControl:MaskedEditValidator ID="validMask" runat="server" ControlToValidate="txtDate" ControlExtender="extMask" Display="Dynamic" ErrorMessage="Invalid Date" InvalidValueMessage="Invalid Date" Width></ajaxControl:MaskedEditValidator>--%>
                    <asp:TextBox ID="txtDate" runat="server" CssClass="input" Width="80px" onkeydown="return EnabledTabKey(this.id, event);" ToolTip="Voucher Date"></asp:TextBox>                    
                    <asp:ImageButton ID="imgDate" runat="server" ImageUrl="~/Images/calendar.gif" />                    
                    <advancedCalender:CalendarExtenderPlus ID="Cal" runat="server"  PopupButtonID="imgDate"  Format="dd/MM/yyyy" TargetControlID="txtDate"></advancedCalender:CalendarExtenderPlus>
                    
                </td>                
                              
            </tr>
            <tr>
                <td colspan="4" valign="top">
                    <div id="div1" runat="server" style="float:left" >Ref. Number &nbsp;&nbsp;<asp:TextBox ID="txtRefNumber" runat="server" CssClass="inputnumber" Width="80px"></asp:TextBox></div> 
                    <asp:RadioButtonList ID="radioCashCredit" runat="server" onclick="return EnableCash();" style="float:left" RepeatDirection="Horizontal">
                        <asp:ListItem Text=" Cash" Value="1" ></asp:ListItem>
                        <asp:ListItem Text=" Credit" Value="0" Selected="True"></asp:ListItem>                     
                    </asp:RadioButtonList>
                    <div id="divCash" runat="server" style="float:left" >&nbsp;&nbsp;&nbsp;Days &nbsp;<asp:TextBox ID="txtCreditDays" runat="server" Text="0" CssClass="inputnumber" MaxLength="3" onkeypress="return isNumberKey(this.id,event);" Width="60px"></asp:TextBox></div> 
                </td>  
            </tr>
        </table>
       </ContentTemplate>
    </asp:UpdatePanel> 
    </div>
    <div style="margin-left:5%; margin-right:5%; margin-top:10px; height:300px; width:90%;">
    <asp:UpdatePanel ID="UpdatePanel2" runat="server">
    <ContentTemplate>
    <asp:HiddenField ID="hidControl" runat="server" Value="2" />
    <table width="100%" style="border:solid 1px #818181;">
        <tr>    
            <td valign="top" style="height:300px;">       
                <table id="tblVouEntry" runat="server" width="100%"  cellpadding="2" cellspacing="0" style="border:solid 1px #818181;margin-bottom:15px">
                 <tr>
                        <td style="text-align:center; width:10%; border-bottom:solid 1px #818181; border-right:solid 1px #818181;" class="headtd">
                           Dr/Cr
                        </td>
                        <td style="text-align:center;width:30%; border-bottom:solid 1px #818181; border-right:solid 1px #818181;" class="headtd">
                            Ledger
                        </td>
                        <td style="text-align:center; width:20%; border-bottom:solid 1px #818181; border-right:solid 1px #818181;" class="headtd">
                            Debit
                        </td>
                        <td style="text-align:center; width:20%; border-bottom:solid 1px #818181; border-right:solid 1px #818181;" class="headtd">
                            Credit
                        </td>
                        <td style="text-align:center; width:20%; border-bottom:solid 1px #818181;" class="headtd">
                            Ledger Balance
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:DropDownList ID="ddlDrCr1" runat="server" CssClass="dropdown" Width="98%" AutoPostBack="true" OnSelectedIndexChanged="ddlDrCr_SelectedIndexChanged" ToolTip="Debit/Credit">
                                <asp:ListItem Text="Cr" Selected="True" Value="1"></asp:ListItem>
                                <asp:ListItem Text="Dr" Value="2"></asp:ListItem>
                            </asp:DropDownList>
                        </td>
                        <td>
                            <asp:DropDownList ID="ddlLedger1" runat="server" CssClass="dropdown" ToolTip="Select Ledger for Transaction" AutoPostBack="true" OnSelectedIndexChanged="ddlLedger_OnSelectedIndexChanged" onchange="return ValidateRepeatLedger(this.id);" Width="98%"></asp:DropDownList>
                        </td>
                        <td>
                            <asp:TextBox ID="txtDebit1" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Debit Amount" onfocus="return RemoveZeroOnFocusDebit(this.id);" onblur="return SetZeroOnBlurDebit();" Enabled="false" onkeypress="return isNumberKey(this.id,event)"></asp:TextBox>
                        </td>
                        <td>
                            <asp:TextBox ID="txtCredit1" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Credit Amount" onfocus="return RemoveZeroOnFocusCredit(this.id);" onblur="return SetZeroOnBlurCredit();"  Text="0.00"  onkeypress="return isNumberKey(this.id,event)"></asp:TextBox>
                        </td>
                        <td>
                            <asp:TextBox ID="txtBalance1" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Ledger Balance" Enabled="false"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:DropDownList ID="ddlDrCr2" runat="server" CssClass="dropdown" Width="98%" AutoPostBack="true" OnSelectedIndexChanged="ddlDrCr_SelectedIndexChanged" ToolTip="Debit/Credit">
                                <asp:ListItem Text="Cr"  Value="1"></asp:ListItem>
                                <asp:ListItem Text="Dr" Selected="True" Value="2"></asp:ListItem>
                            </asp:DropDownList>
                        </td>
                        <td>
                            <asp:DropDownList ID="ddlLedger2" runat="server" CssClass="dropdown" ToolTip="Select Ledger for Transaction" AutoPostBack="true" OnSelectedIndexChanged="ddlLedger_OnSelectedIndexChanged" onchange="return ValidateRepeatLedger(this.id);" Width="98%"></asp:DropDownList>
                        </td>
                        <td>
                            <asp:TextBox ID="txtDebit2" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Debit Amount" onfocus="return RemoveZeroOnFocusDebit(this.id);" onblur="return SetZeroOnBlurDebit();"  Text="0.00"  onkeypress="return isNumberKey(this.id,event)"></asp:TextBox>
                        </td>
                        <td>
                            <asp:TextBox ID="txtCredit2" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Credit Amount" onfocus="return RemoveZeroOnFocusCredit(this.id);" onblur="return SetZeroOnBlurCredit();" Enabled="false"   onkeypress="return isNumberKey(this.id,event)"></asp:TextBox>
                        </td>
                        <td>
                            <asp:TextBox ID="txtBalance2" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Ledger Balance" Enabled="false"></asp:TextBox>
                        </td>
                    </tr>
                </table> 
             </td>
        </tr>           
        <tr>
            <td style="border:solid 1px #818181; background-color:#f2f2f2">   
                <table width="100%" cellpadding="2" cellspacing="0">
                 <tr>
                        <td style="text-align:right;width:40%;">
                            Total
                        </td>
                        <td style="text-align:center;width:20%;">
                            <asp:TextBox ID="txtTotalDebit" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Total Debit Amount" Text="0.00" onkeypress="return false" onkeydown="return false"></asp:TextBox>
                        </td>
                        <td style="text-align:center; width:20%;">
                            <asp:TextBox ID="txtTotalCredit" runat="server" CssClass="inputnumber" Width="98%" ToolTip="Total Credit Amount" Text="0.00" onkeypress="return false" onkeydown="return false"></asp:TextBox>
                        </td>
                        <td style="text-align:center; width:20%;">
                            &nbsp;
                        </td>
                    </tr>                                            
                </table>  
                <table width="100%" cellpadding="2" cellspacing="0">
                 <tr>
                        <%--<td style="text-align:right;width:11%;">
                            Cheque No.
                        </td>
                        <td style="text-align:left;width:18%;">
                            <asp:TextBox ID="txtChequeNo" runat="server" ToolTip="Cheque Number" CssClass="input" Width="98%"></asp:TextBox>
                        </td>--%>
                        <td style="text-align:right;width:12%;">
                            Narration
                        </td>
                        <td style="text-align:left; width:88%;">
                            <asp:TextBox ID="txtNarration" runat="server" ToolTip="Narration" CssClass="input" Width="98%"></asp:TextBox>
                        </td>
                    </tr>                                            
                </table>                   
                <div style="margin-left:5%; margin-right:5%; margin-top:5px; width:90%; margin-bottom:5px">
                    <asp:Button ID="btnAddRow" runat="server" ToolTip="Add New Row!"  Text="Add Row" CssClass="button" OnClick="btnAddRow_Click" />&nbsp;
                    <asp:Button ID="btnDeleteRow" runat="server" ToolTip="Delete Last Row!" Text="Delete Row" CssClass="button1" OnClientClick="return confirmDelete();" OnClick="btnDeleteRow_Click" />&nbsp;
                    <asp:Button ID="btnSave" runat="server" Text="Save" ToolTip="Save Contra Transaction!" CssClass="button" OnClientClick="return validateAmt();" OnClick="btnSave_Click" />&nbsp;
                    <asp:Button ID="btnClose" runat="server" Text="Close" ToolTip="Close Current Page!" CssClass="button" OnClick="btnClose_Click" />&nbsp;
                </div>
            </td>
        </tr>
     </table>
       </ContentTemplate>
    </asp:UpdatePanel>          
    </div>
    
    </form>
</body>
</html>
